package com.feng.controller;

import com.alibaba.excel.EasyExcel;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.AdminAddCategoryDto;
import com.feng.domain.dto.AdminCategoryDto;
import com.feng.domain.entity.Category;
import com.feng.domain.vo.CategoryExportVo;
import com.feng.enums.AppHttpCodeEnum;
import com.feng.service.CategoryService;
import com.feng.utils.BeanCopyUtil;
import com.feng.utils.ObjectMapperUtils;
import com.feng.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        return categoryService.listAllCategory();
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            WebUtils.excelResponseHeader(response, "分类.xlsx");
            List<Category> categories = categoryService.list();
            List<CategoryExportVo> exportVo = BeanCopyUtil.articleListVo(categories, CategoryExportVo.class);
            EasyExcel.write(response.getOutputStream(), CategoryExportVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(exportVo);
        } catch (Exception e) {
            response.reset();
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, ObjectMapperUtils.getObjectJson(result));
        }
    }

    @PreAuthorize("@ps.hasPermission('content:category:list')")
    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, String name, String status){
        return categoryService.getList(pageNum, pageSize, name, status);
    }

    @PreAuthorize("@ps.hasPermission('content:category:list')")
    @PostMapping
    public ResponseResult addCategory(@RequestBody AdminAddCategoryDto categoryDto){
        return categoryService.addCategory(categoryDto);
    }

    @PreAuthorize("@ps.hasPermission('content:category:list')")
    @GetMapping("/{id}")
    public ResponseResult getUpdateList(@PathVariable("id") Long id){
        return categoryService.getUpdateList(id);
    }

    @PreAuthorize("@ps.hasPermission('content:category:list')")
    @PutMapping
    public ResponseResult updateCategory(@RequestBody AdminCategoryDto categoryDto){
        return categoryService.updateCategory(categoryDto);
    }

    @PreAuthorize("@ps.hasPermission('content:category:list')")
    @DeleteMapping("/{id}")
    public ResponseResult delCategory(@PathVariable("id") List<Long> ids){
        return categoryService.delCategory(ids);
    }
}